package com.epfresh.supplier.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.epfresh.api.adapter.CommonAdapter;
import com.epfresh.api.adapter.ViewHolder;
import com.epfresh.api.entity.RequestEntity;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.http.HttpRequest;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.utils.T;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.LogisticsGroupInfoEntity;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderLogisticsGroupInfoActivity extends BaseActivity {

    static final String TAG = "OrderLogisticsInfoActivity";
    String orderId,phone;

    TextView tv_logistics_group_number,tv_logistics_group_time;
    TextView tv_logistics_group_driver,tv_logistics_group_car,tv_logistics_group_car_no,tv_logistics_group_take_phone;
    View rl_receiver_info;
    ListView lv;
    ArrayList<LogisticsGroupInfoEntity.NodesBean> list = new ArrayList<>();
    LogisticsAdapter logisticsAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        setToolbarTitle("订单跟踪");
        orderId = getIntent().getStringExtra("orderId");
        if(orderId==null) {
            T.toast("数据有误，请重试");
            finish();
        }
        mHttpInfo();
    }
    private void initView() {
        setContentView(R.layout.activity_order_group_logisticsinfo);
        rl_receiver_info = findViewById(R.id.rl_receiver_info);
        tv_logistics_group_number = (TextView)findViewById(R.id.tv_logistics_group_number);
        tv_logistics_group_time = (TextView)findViewById(R.id.tv_logistics_group_time);
        tv_logistics_group_take_phone = (TextView)findViewById(R.id.tv_logistics_group_take_phone);
        tv_logistics_group_car = (TextView)findViewById(R.id.tv_logistics_group_car);
        tv_logistics_group_car_no = (TextView)findViewById(R.id.tv_logistics_group_car_no);
        tv_logistics_group_driver = (TextView)findViewById(R.id.tv_logistics_group_driver);
        tv_logistics_group_take_phone = (TextView)findViewById(R.id.tv_logistics_group_take_phone);
        tv_logistics_group_take_phone.setOnClickListener(this);

        lv = (ListView)findViewById(R.id.lv);
        logisticsAdapter = new LogisticsAdapter(this,list,R.layout.item_money_and_logistics);
        lv.setAdapter(logisticsAdapter);
        lv.setDivider(null);
        tvBack.setVisibility(View.VISIBLE);
        baseback.setVisibility(View.VISIBLE);
        baseback.setOnClickListener(this);
    }
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.baseback:
                // 返回上一页
                finish();
                break;

            case R.id.tv_logistics_group_take_phone:
                Intent call = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));//调出来拨打页面；
                startActivity(call);
                break;
/*            case R.id.tv_receiver_phone:
                Intent phone = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + tvReceiverPhone.getText().toString()));//调出来拨打页面；
                startActivity(phone);*/
            default:
                break;
        }
    }
    class LogisticsAdapter extends CommonAdapter<LogisticsGroupInfoEntity.NodesBean> {
        public LogisticsAdapter(Context context, List<LogisticsGroupInfoEntity.NodesBean> historyList, int a) {
            super(context, historyList, a);
        }

        @Override
        public void convert(ViewHolder holder, final LogisticsGroupInfoEntity.NodesBean bean) {
            holder.setText(R.id.tv_content, bean.getInfo());
            holder.setText(R.id.tv_time, bean.getTime());
            if(list.indexOf(bean)==list.size()-1){
                holder.getView(R.id.view_2).setVisibility(View.INVISIBLE);
                holder.setTextColor(R.id.tv_content, ContextCompat.getColor(OrderLogisticsGroupInfoActivity.this,R.color.TextColordarker));
            }
            if(list.indexOf(bean)==0){
                holder.getView(R.id.view_1).setVisibility(View.INVISIBLE);
                holder.setTextColor(R.id.tv_content,ContextCompat.getColor(OrderLogisticsGroupInfoActivity.this,R.color.coupon_active));
                ((ImageView)holder.getView(R.id.iv_state)).setImageResource(R.mipmap.order_red_dot);
            }
        }
    }
    /** 查看物流  */
    private void mHttpInfo() {
        RequestEntity<Map<String, String>> data = new RequestEntity<>();
        data.setCmd("pm-order/baseorder/order/node");//baseorder/order/node
        Map<String, String> map = new HashMap<>();
        map.put("orderId", orderId);
        data.setParameters(map);
        request(data, "pm-order/baseorder/order/node", new OnRequestListener<LogisticsGroupInfoEntity>() {
            @Override
            public LogisticsGroupInfoEntity jsonToObj(String responseStr) {
                return new Gson().fromJson(responseStr, LogisticsGroupInfoEntity.class);
            }

            @Override
            public void onFail(int failCode, Object arg, Object tag) {
                hideProgressDialog();
                showExceptionView(R.mipmap.upload_error, "网路错误", HttpRequest.FAIL_NET_FAULT);
            }

            @Override
            public void onResponseError(int failCode, Object arg, Object tag) {
                hideProgressDialog();
                showExceptionView(R.mipmap.upload_error, arg + "", HttpRequest.FAIL_NET_FAULT);
            }
            @Override
            public void onStart(Object tag) {
                showProgressDialog();
            }

            @Override
            public void onResponse(ResponseEntity<LogisticsGroupInfoEntity> response, Object tag) {
                hideProgressDialog();
                LogisticsGroupInfoEntity responseElement = response.getResponseElement();
                updateOrderInfo(responseElement);
            }

        });
    }
    void updateOrderInfo(LogisticsGroupInfoEntity moneyPath){
        phone = moneyPath.getDisContactPhone();

        if(moneyPath.getEstimateArriveTime()==null||"".equals(moneyPath.getEstimateArriveTime())){
            tv_logistics_group_time.setVisibility(View.GONE);
        }else {
            tv_logistics_group_time.setVisibility(View.VISIBLE);

        }
        tv_logistics_group_time.setText("预计到货日期："+moneyPath.getEstimateArriveTime());
        tv_logistics_group_number.setText("单号："+moneyPath.getOrderNo());
        tv_logistics_group_driver.setText(moneyPath.getDisContacter());
        tv_logistics_group_car.setText(moneyPath.getDeliveryWay());
        tv_logistics_group_car_no.setText(moneyPath.getVehicleNo());

        list.addAll(moneyPath.getNodes());
        logisticsAdapter.notifyDataSetChanged();

        if(moneyPath.getDisContacter()==null||"".equals(moneyPath.getDisContacter())){
            rl_receiver_info.setVisibility(View.GONE);
        }else {
            rl_receiver_info.setVisibility(View.VISIBLE);
        }
    }

}
